import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { DNavBar, DInformation,DModal } from '@/components'
import http from '@/api'
import sdk from "@/common/sdk"
import './information.scss'

export default class DInformationBox extends Component {

    constructor(props) {
        super(props)
        this.state = {
            title: '企业认证',
            status: '',
            list: ['登记企业并入驻平台，拥有企业管理员权限', '可发布寻车、车源、运输信息', '可发起订金担保、运输下单', '可管理企业员工'],
            text: '立即认证',
            usertitle: '员工认证',
            userstatus: '',
            userlist: ['加入一家已认证的企业，拥有员工权限', '可发布寻车、车源、运输信息', '可发起运输下单'],
            usertext: '立即认证',
            isClick: '1',
            isClick1: '1',
            isClick2: '1',
            navtitle: '信息认证'
        }
    }
    // 退出登录
    onOutLogin() {
    	  http.logout().then(res=>{
    		    Taro.showToast({
    		      title: '退出登录',
    		      duration: 800
    		    }).then(() => {
    		      localStorage.removeItem("X-Auth0-Token")

    		      setTimeout(() => {
    				  if (window.bridge) {
    		        window.bridge.callHandler('logout', {}, function responseCallback(res) { })
    				}
    		      }, 1000)
    		    })
    		   
    	  })
      
    }
	onFirstHandle(){
		this.setState({
			isformatShow:false
		})
	}
    onLastHandle(){
		this.onOutLogin()
		this.onFirstHandle()
	}
    componentWillMount() {
        this.getConfirmInformation()
    }
    componentDidShow() {
        this.getConfirmInformation()
        sdk.setTitleInfo(this.state.navtitle)
    }
    getStatusText(status) {
        return {
            $0: '待审核',
            $1: '认证成功',
            $2: '认证失败'
        }[`$${status}`]
    }
    getText(status) {
        return {
            $0: '查看详情',
            $2: '重新认证',
            $3: '立即认证'
        }[`$${status}`]
    }
    getConfirmInformation() {
		let { isformatShow } = this.state
        http.getConfirmInfo().then(res => {
            let { isClick1, isClick2 } = this.state
            if (res.isClick === '2' || res.isClick === '4') {
                isClick1 = '2'
                // this.setState({
                //     isClick1: '2'
                // })
            }
            if (res.isClick === '2' || res.isClick === '3') {
                isClick2 = '2'
                // this.setState({
                //     isClick2: '2'
                // })
            }
			if(+res.eaIsAppro===1){
				isformatShow = true
			}
            this.setState({
                isClick: res.isClick,
				isformatShow,
                status: res.eaIsAppro,
                userstatus: res.imIsAppro,
                entId: res.entId,
                myId: res.myId,
                text: this.getText(res.eaIsAppro) || '立即认证',
                usertext: this.getText(res.imIsAppro) || '立即认证',
                isClick1,
                isClick2
            })
        })
    }

    onHandleClick(status, type) {
        let { isClick, entId, myId } = this.state

        if (type === 1 && (isClick === '1' || isClick === '3')) {
            if (status === '0') {
                Taro.navigateTo({
                    url: `/pages/enterprisedetail/enterprisedetail?id=${entId}`
                })
            } else if (status === '2') {
                Taro.navigateTo({
                    url: `/pages/enterpriseform/enterpriseform?entId=${entId}`
                })
            } else {
                Taro.navigateTo({
                    url: '/pages/enterpriseform/enterpriseform'
                })
            }

        } else if (type === 2 && (isClick === '1' || isClick === '4')) {
            if (status === '0') {
                Taro.navigateTo({
                    url: `/pages/userdetail/userdetail?id=${myId}`
                })
            } else if (status === '2') {
                Taro.navigateTo({
                    url: `/pages/userform/userform?myId=${myId}`
                })
            } else {
                Taro.navigateTo({
                    url: '/pages/userform/userform'
                })
            }
        } else {
            return
        }
    }

    render() {
        const {
            title,
            status,
            list,
            text,
            usertitle,
            userstatus,
            userlist,
            usertext,
            isClick1,
            isClick2,
            navtitle,
			isformatShow  = false
        } = this.state
        return (
            <View className='b_information'>
                <DNavBar title={navtitle}></DNavBar>
                <View className='b_information__box'>
                    <View className='b_information__content'>
                        <DInformation title={title} isClick={isClick1} status={this.getStatusText(status)} list={list} text={text} onHandleClick={this.onHandleClick.bind(this, status, 1)}></DInformation>
                        {/*<View className='item_user__box'>
                            <DInformation title={usertitle} isClick={isClick2} status={this.getStatusText(userstatus)} list={userlist} text={usertext} onHandleClick={this.onHandleClick.bind(this, userstatus, 2)}></DInformation>
                        </View> */}
                    </View>
                </View>
				<DModal isShow={isformatShow} title="提示" text="您的认证已审核通过，请退出重新登录" firstbtn="取消" lastbtn="确定" onFirst={this.onFirstHandle.bind(this)}  onLast={this.onLastHandle.bind(this)}></DModal>
            </View>
        )
    }
}